#!/bin/bash
getTomcatPid ()
{
	Tomcat_pid=`ps -ef|grep -w Bootstrap|grep -v grep|awk '{printf $2}'`
	return $Tomcat_pid
}

getTomcatPid

# Getting parameters:
while getopts "w:c:h" OPT;do
case $OPT in
"w")warning=$OPTARG;;
"c")critical=$OPTARG;;
"h")help;;
esac
done

Survivor0_Total=`su --shell=/bin/bash tomcat -c "jstat -gc $Tomcat_pid"|tail -1|awk '{print $1}'`

Survivor0_Used=`su --shell=/bin/bash tomcat -c "jstat -gc $Tomcat_pid"|tail -1|awk '{print $3}'`

Survivor0_unused=$(echo "scale=2;($Survivor0_Total - $Survivor0_Used)/$Survivor0_Total" | bc)

Survivor0_unused_percentage=$(echo "$Survivor0_unused*100" | bc)

if [ $(echo "$Survivor0_unused < $critical"|bc) -eq 1 ];then
	msg="CRITICAL"
	status=2
else if [ $(echo "$Survivor0_unused < $warning"|bc) -eq 1 ];then
	msg="WARNING"
	status=1
else
	msg="OK"
	status=0
	fi
fi

echo "$msg - Survivor0_Unused Memory Ratio $Survivor0_unused_percentage%"

exit $status